# -*- coding:utf-8 -*-
import logging
import time

from selenium.common.exceptions import ElementClickInterceptedException
from selenium.webdriver.common.by import By

from PageClass.common.boeCommon import BoeCommon
from PageClass.ecsPageClass.ecsIndexPage import EcsIndexPage
from Util import logger


class Class_NewFundCollectionBoe(EcsIndexPage, BoeCommon):
    def __init__(self, driver):
        super().__init__(driver)

    # 打开流水管理页面
    def goToBoePage(self, url):
        self.driver.get(url)
        logger.info("打开下一个页面")

    # 点击'手工添加'按钮
    def clickAddByHandle(self):
        time.sleep(2)
        _button = (By.XPATH, f'//div[@id="pane-inFlow"]/div/div/div[2]/button/span[contains(text(), "手工添加")]')
        self.click(*_button)

    # 手工添加弹框填充数据
    def _send_values_by_text(self, label, text):
        _element = (By.XPATH, f'//div[@class="el-dialog__body"]/div/form/div/div/div/div/input')
        self.send_text(text, *_element)
        logger.info(f"手工添加弹框输入{label}字段")

    # '手工添加'弹框-银行流水号
    def input_hostid(self, text):
        _element = (By.ID, 'form_hostid')
        # self.send_text(text, *_element)
        self.driver.find_element(*(_element)).send_keys(str(text))
        logger.info(f"手工添加弹框输入银行流水号字段：{text}")

    # '手工添加'弹框-收款方账号
    def click_receiptAccount(self):
        _element = (By.ID, 'form_receiptAccount')
        self.click(*_element)

    # '手工添加'弹框-金额
    def input_itemamount(self, text):
        _element = (
        By.XPATH, '//div[@class="zte-item amount"]/div/input[1]')  # //div[@class="zte-item amount"]/div/input[2]
        # self.send_text(str(text), *_element)
        self.driver.find_element(*(_element)).send_keys(str(text))
        logger.info(f"手工添加弹框输入金额字段：{text}")

    # '手工添加'弹框-交易时间
    def input_actDate(self, text):
        _element = (By.ID, 'form_actDate')
        self.send_text(str(text), *_element)
        logger.info(f"手工添加弹框输入交易时间字段：{text}")

    # '手工添加'弹框-摘要
    def input_explainInfo(self, text):
        _element = (By.ID, 'form_explainInfo')
        self.send_text(str(text), *_element)
        logger.info(f"手工添加弹框输入摘要字段：{text}")

    # '手工添加'弹框-提交按钮
    def click_submit_button(self):
        _element = (By.XPATH, '//div[@class="el-dialog__body"]/div/form/div/div/button[2]')
        self.click(*_element)
        logger.info(f"点击手工添加弹框提交按钮")
        time.sleep(2)

    # 流水认领-条件查询-展开
    def click_bigger_window(self):
        _element = (
        By.XPATH, '/html/body/div/section/main/div[2]/div/div/div[2]/div[1]/div/div/div[1]/div/button/span/i[1]')
        self.click(*_element)
        logger.info(f"点击流水认领-条件查询-展开按钮")

    # 流水清分
    def input_undefined_hostId(self, hostId):
        _element = (By.ID, 'undefined_hostid')
        self.send_text(hostId, *_element)
        logger.info(f'输入查询条件-流水id：{hostId}')

    # 流水认领-查询条件-摘要
    def input_undefined_explainInfo(self, text):
        # _element = (By.ID, 'undefined_explainInfo')
        _element = (By.XPATH,
                    '/html/body/div[1]/section/main/div[2]/div/div/div[2]/div[1]/div/div/div[1]/form/div[13]/div/div/div/input')
        self.send_text(str(text), *_element)
        logger.info(f"流水认领-查询条件-摘要输入摘要字段：{text}")

    # 流水认领-查询条件-查询按钮
    def click_search_button(self):
        _element = (By.XPATH, '//button[@class="el-button el-button--primary el-button--small"]')
        self.click(*_element)

    # 选第一行
    def click_allselect(self):
        for _ in range(3):
            try:
                _element = (By.XPATH, '//tbody/tr[1]/td[@class="el-table_1_column_1 is-center "]/div/label/span/span')
                self.click(*_element)
            except:
                if _ == 2:
                    logging.info("流水发布-选择第一行流水失败")
                    raise ElementClickInterceptedException
            else:
                logging.info("成功选择了第一行流水，将进行流水发布")
                return

    # 流水发布
    def click_fbbutton(self):
        _element = (
        By.XPATH, '/html/body/div[1]/section/main/div[2]/div/div/div[2]/div[1]/div/div/div[2]/button[2]/span')
        self.click(*_element)

    # 确认发布
    def click_double_ok_button(self):
        _element = (By.XPATH,
                    '/html/body/div[@class="el-message-box__wrapper"]/div/div[@class="el-message-box__btns"]/button[@class="el-button el-button--default el-button--small el-button--primary "]')
        self.click(*_element)


    def wait_table_or_page1(self):
        s = time.time()
        _ele = (By.XPATH, '/html/body/div[1]/section/main/div[2]/div/div/div[2]/div[1]/div/div[1]/div/button/span/i[1]')
        if self.find_element(*_ele):
            logger.info("加载页面成功")
            return
        while True:
            if time.time() - s > 60:
                logger.error("加载页面异常")
                raise Exception("加载页面异常")
            try:
                if self.find_element(*_ele):
                    logger.info("加载页面成功")
                    return
                else:
                    logger.warning("加载页面失败，再来")
            except:
                pass


    # 待认领菜单-条件查询-展开
    def click_bigger_window_e(self):
        _element = (
        By.XPATH, '/html/body/div[1]/section/main/div[2]/div/div/div[2]/div[1]/div/div[1]/div/button/span/i[1]')
        self.click(*_element)
        logger.info(f"点击流水认领-条件查询-展开按钮")

    # 待认领菜单-查询条件-摘要
    def input_undefined_explainInfo_e(self, text):
        _element = (By.XPATH,
                    '/html/body/div[1]/section/main/div[2]/div/div/div[2]/div[1]/div/div[1]/form/div[10]/div/div/div/input')
        self.send_text(str(text), *_element)
        logger.info(f"待认领菜单-查询条件-摘要 输入摘要字段：{text}")

    # 待认领菜单-查询条件-流水状态
    def click_claimStatus(self):
        _element = (By.XPATH,
                    '//div[@id="pane-IN"]/div/div[@class="search-body"]/form/div[@class="el-form-item el-form-item--smal'
                    'l claimStatus"]/div/div/div/div/input[@id="undefined_claimStatus" and @class="el-input__inner"]')
        self.click(*_element)
        _ele = (By.XPATH, '/html/body/div[@class="el-select-dropdown el-popper"]/div/div/ul/li[2]/div/span[contains(text(), "待确认")]')
        self.click(*_ele)

    # 待认领菜单-查询条件-查询按钮
    def click_search_button_e(self):
        _element = (By.XPATH, '//button[@class="el-button el-button--primary el-button--small"]')
        self.click(*_element)

    # 点击第一行的来款确认按钮,进入流水详情页面
    def click_double_moneyok_button(self):
        _element = (By.XPATH, '/html/body/div[1]/section/main/div[2]/div/div/div[2]/div[1]/div/div[3]/div/div[5]/'
                              'div[2]/div/div[2]/table[1]/tbody/tr/td/div/button[2]/span')
        for _ in range(3):
            try:
                self.click(*_element)
            except:
                if _ == 2:
                    logging.info("点击第一行的来款确认失败")
                    raise ElementClickInterceptedException
            else:
                logging.info("成功点击第一行的来款确认，将进入流水详情页面")
                return

        # 切换最新打开的窗口句柄
        # 获取所有的窗口句柄
        wins = self.driver.window_handles
        # 切换到新的窗口
        self.driver.switch_to.window(wins[-1])
        logger.debug(f"切换之后的句柄：{self.driver.current_window_handle}")

    def wait_table_or_page2(self):
        s = time.time()
        _ele = (By.ID, 'undefined_oppositeId')
        if self.find_element(*_ele):
            logger.info("加载页面成功")
            return
        while True:
            if time.time() - s > 60:
                logger.error("加载页面异常")
                raise Exception("加载页面异常")
            try:
                if self.find_element(*_ele):
                    logger.info("加载页面成功")
                    return
                else:
                    logger.warning("加载页面失败，再来")
            except:
                pass

    # 流水详情页面-本次确认信息-往来方字段
    def click_goandto(self):
        _element = (By.ID, 'undefined_oppositeId')
        self.click(*_element)

    # 流水详情页面-本次确认信息-本次确认金额字段
    def input_thistime_money(self, text):
        _element = (
        By.XPATH, '//div[@id="form_actAmount" and @class="input-currency el-input el-input-group el-input--small"]')
        self.send_text(str(text), *_element)
        logger.info(f"流水详情页面-本次确认信息-本次确认金额字段：{text}")

    # 流水详情页面-本次确认信息-款项类型字段
    def click_moneytype(self):
        _element = (By.XPATH, '//input[@id="form_operationType" and @class="el-input__inner"]')
        self.click(*_element)

    # 流水详情页面-本次确认信息-备注字段
    def input_this_remark(self, text):
        _element = (By.XPATH, '//textarea[@id="form_memo" and @class="el-textarea__inner"]')
        self.send_text(str(text), *_element)
        logger.info(f"待认领菜单-查询条件-摘要 输入摘要字段：{text}")

    # 流水详情页面-本次确认信息-流水确认按钮
    def input_this_ok_button(self):
        _element = (By.XPATH, '//button[@class="el-button el-button--primary el-button--medium"]')
        self.click(*_element)
        logger.info(f"流水详情页面-本次确认信息-流水确认按钮")


    # 勾选待认领的收款拆分行勾选框
    def click_flex_1(self):
        _element = (
        By.XPATH, '/html/body/div/section/main/div[2]/div/div[2]/div[3]/div[1]/div[1]/div/div[1]/label/span/span')
        self.click(*_element)
        logger.info(f"勾选待认领的收款拆分行勾选框")

    # 点击流水认领按钮
    def click_this_get_button(self):
        _element = (By.XPATH, '//button[@class="el-button el-button--primary el-button--medium"]')
        self.click(*_element)
        logger.info(f"点击流水认领按钮")
        # 切换最新打开的窗口句柄
        # 获取所有的窗口句柄
        wins = self.driver.window_handles
        # 切换到新的窗口
        self.driver.switch_to.window(wins[-1])
        logger.debug(f"当前窗口句柄号为：{self.getCurrentWindowHandle()}，标题为：{self.currentTitle}，url为：{self.currentUrl}")

    # 点击跳过核销按钮
    def click_InButton(self):
        _element = (By.XPATH, '//div[@class="el-message-box__btns"]/button/span[contains(text(),"跳过核销")]')
        self.click(*_element)
        logger.info(f"点击跳过核销按钮")
        # 获取所有的窗口句柄
        wins = self.driver.window_handles
        # 切换到新的窗口
        self.driver.switch_to.window(wins[-1])
        logger.debug(f"当前窗口句柄号为：{self.getCurrentWindowHandle()}，标题为：{self.currentTitle}，url为：{self.currentUrl}")


    def wait_table_or_page3(self):
        logger.info(f"wait_table_or_page3开始")
        logger.debug(f"当前窗口句柄号为：{self.getCurrentWindowHandle()}，标题为：{self.currentTitle}，url为：{self.currentUrl}")
        s = time.time()
        _ele = (By.XPATH, '//input[@id="cost.0.operationSubTypeId" and @class="el-input__inner"]')
        if self.find_element(*_ele):
            logger.info("加载页面成功")
            return
        while True:
            if time.time() - s > 120:
                logger.error("加载页面异常,超时")
                self.switchWindow()
                # return
                raise Exception("加载页面异常")
            try:
                if self.find_element(*_ele):
                    logger.info("加载页面成功")
                    return
                else:
                    logger.warning("加载页面失败，再来")
            except:
                pass

    # 收款报账单填单页面-各区域的业务小类选择
    def click_operationSubType(self, name: str, money: str or int):
        logger.info(f"收款报账单填单页面-各区域的业务小类选择>>>")
        logger.debug(f"当前窗口句柄号为：{self.getCurrentWindowHandle()}，标题为：{self.currentTitle}，url为：{self.currentUrl}")
        if name == '费用明细':
            # 点击业务小类字段
            _ele = (By.XPATH, '//input[@id="cost.0.operationSubTypeId" and @class="el-input__inner"]')
            self.click(*_ele)
            time.sleep(1)
            # 选中下拉框第一个
            _element = (By.ID, 'cost.0.operationSubTypeId.option.0')
            self.click(*_element)
            time.sleep(1)
            # 给金额弹框填值
            _element_m = (By.ID, 'cost.0.expenseAmount')
            self.send_text(str(money), *_element_m)
        elif name == '登记预收':
            # 点击业务小类字段
            _ele = (By.XPATH, '//input[@id="advancePay.0.operationSubTypeId" and @class="el-input__inner"]')
            self.click(*_ele)
            # 选中下拉框第一个
            _element = (By.ID, 'advancePay.0.operationSubTypeId.option.0')
            self.click(*_element)
            time.sleep(1)
            # 给金额弹框填值
            _element_m = (By.ID, 'advancePay.0.expenseAmount')
            self.send_text(str(money), *_element_m)
        elif name == '登记收入':
            # 点击业务小类字段
            _ele = (By.XPATH, '//input[@id="income.0.operationSubTypeId" and @class="el-input__inner"]')
            self.click(*_ele)
            # 选中下拉框第一个
            _element = (By.ID, 'income.0.operationSubTypeId.option.0')
            self.click(*_element)
            time.sleep(1)
            # 给金额弹框填值
            _element_m = (By.ID, 'income.0.expenseAmount')
            self.send_text(str(money), *_element_m)
            # 还需单独写选择成本中心
        else:
            logger.warning("name收款报账单单据区域输入有误")

    # 收款报账单填单页面-登记收入-删除行
    def click_del_3(self):
        _element = (By.XPATH,
                    '//div[@class="step-bill-area bill-area step area-is-expand table income"]/div/div/div/div/form/div[@class="el-form-item operate area-control el-form-item--small"]/div/button[2]')
        self.click(*_element)

    # 收款报账单填单页面-登记收入-成本中心
    def click_oldmoney_center(self):
        _element = (By.ID, 'income.0.ccId')
        self.click(*_element)

    # 收款报账单填单页面-登记收入-成本中心small弹框关闭按钮
    def click_close_button(self):
        _element = (By.XPATH, "//button[@class='el-button el-button--default el-button--small']/span[text()='关闭']")
        self.click(*_element)

    # 收款报账单填单页面-下一步
    def click_nextstep_button(self):
        _element = (By.XPATH, '//button[@class="el-button el-button--primary el-button--small"]')
        self.click(*_element)
        # 切换最新打开的窗口句柄
        # 获取所有的窗口句柄
        wins = self.driver.window_handles
        # 切换到新的窗口
        self.driver.switch_to.window(wins[-1])
        logger.debug(f"切换之后的句柄：{self.driver.current_window_handle}")

    # 收款报账单单据预览页面-提交
    def click_end_button(self):
        logger.info("点击提交单据")
        _element = (By.XPATH, '//button[@class="el-button el-button--primary el-button--small"]')
        self.click(*_element)
        message = None
        try:
            message = self.getBoeSuccessMessage()
        except Exception as e:
            self.click_ignoreButton()
            message = self.getBoeSuccessMessage()
        finally:
            return message

    BoeSuccessMsg = (By.XPATH, '//*[@class="el-message-box__message"]')

    def getBoeSuccessMessage(self):
        # 提单成功结果页面提示语
        return self.get_elementText(*self.BoeSuccessMsg)
